#include "stdafx.h"
#include "DataControl.h"

CDataControl::CDataControl( CString iniType, CString key )
{
	this->iniType = iniType;
	this->key = key;
}
CDataControl::~CDataControl(void)
{
}

void CDataControl::setClipboardText( HWND hwnd, CString value )
{
	if(::OpenClipboard(hwnd) && ::EmptyClipboard())
	{
		HGLOBAL global = ::GlobalAlloc(GHND, value.GetLength());
		LPVOID lock = ::GlobalLock(global);
		::SetClipboardData(CF_TEXT, lock);
		::CloseClipboard();
		::GlobalFree(global);
	}
}

CString CDataControl::getClipboardText( HWND hwnd )
{
	if(::IsClipboardFormatAvailable(CF_TEXT) && ::OpenClipboard(hwnd))
	{
		HGLOBAL hClipMemor = ::GetClipboardData(CF_TEXT);
		LPBYTE lock =  (LPBYTE)::GlobalLock(hClipMemor);
		CString str = CString(lock);
		::GlobalUnlock(hClipMemor);
		::CloseClipboard();
		return str;
	}
	return NULL;
}

void CDataControl::showMenu( UINT id, int menuId, CWnd wnd)
{
	CMenu menu;
	menu.LoadMenu(id);
	POINT point;
	GetCursorPos(&point);
	menu.GetSubMenu(menuId)->TrackPopupMenu(0, point.x, point.y, &wnd);

}
/************************************************************************/
/*                                                                      */
/************************************************************************/

void CAdvancedEdit::OnKeyUp( UINT nChar, UINT nRepCnt, UINT nFlags )
{
	CString value;
	GetWindowText(value);
	iniAccessor.setString(iniType, key, value);
	CEdit::OnKeyUp(nChar, nRepCnt, nFlags);
}

int CAdvancedEdit::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
	CString value = iniAccessor.getString(iniType, key);
	SetWindowText(value);
	return CEdit::OnCreate(lpCreateStruct);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
void CAdvancedDialog::setTopMost(bool isTop)
{
	SetWindowPos(isTop ? &wndTopMost : &wndNoTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}

CAdvancedDialog::CAdvancedDialog( CWnd* pParent /*= NULL*/ )
{
}

LPCSTR CAdvancedDialog::openFileDialog( LPCTSTR fileFilter )
{
	CFileDialog fileDialog(TRUE, NULL, NULL, 0, fileFilter);
	int resultCode = fileDialog.DoModal();
	if(resultCode == IDOK)
	{
		return fileDialog.GetPathName();
	}
	return NULL;
}
/************************************************************************/
/*                                                                      */
/************************************************************************/
CAdvancedTabCtrl::CAdvancedTabCtrl( CWnd* pParent /*= NULL*/ )
{
	currentTabIndex = 0;
}

void CAdvancedTabCtrl::addNewTab( LPCSTR title,CDialog* dialog)
{
	int index = GetItemCount() + 1;
	InsertItem(index, title);
	dialog->SetParent(this);
	dialogList[index] = dialog;
	dialog->ShowWindow(index == 0);
}

void CAdvancedTabCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
	int index = GetCurFocus();
	if(index != currentTabIndex)
	{
		dialogList[currentTabIndex]->ShowWindow(FALSE);
		dialogList[index]->ShowWindow(TRUE);
		currentTabIndex = index;
	}
	CTabCtrl::OnLButtonDown(nFlags, point);
}

void CAdvancedTabCtrl::moveToRight(CDialog* dialog)
{
	RECT rect;
	GetClientRect(&rect);
	rect.top+=20;
	rect.bottom-=4;
	rect.left+=4;
	rect.right-=4;
	dialog->MoveWindow(&rect);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/

void CAdvancedListCtrl::loadDirectory( CString directoryPath )
{
	imageList.Create(32, 32, ILC_COLOR | ILC_COLOR32, 0, 0);
	this->SetImageList(&imageList, LVSIL_NORMAL);
	CFileFind fileFind;
	int hasFind = fileFind.FindFile(directoryPath);
	int i = 0;
	while (hasFind)
	{
		if(fileFind.IsDirectory())
		{
			continue;
		}
		SHFILEINFO info;
		SHGetFileInfo(fileFind.GetFilePath(), 0, &info, sizeof(info), SHGFI_ICON);
		this->InsertItem(i++, fileFind.GetFileName(), imageList.Add(info.hIcon));
		hasFind = fileFind.FindNextFile();
	}
	fileFind.Close();
}

void CAdvancedListCtrl::clean()
{
	DeleteAllItems();
	CHeaderCtrl* headCtrl = GetHeaderCtrl();
	int columnCount = headCtrl->GetItemCount();
	for (int i = columnCount; i >= 0; i--)
	{
		DeleteColumn(i);
	}
}
/************************************************************************/
/*                                                                      */
/************************************************************************/
void CAdvancedTreeCtrl::loadDirectory( CString directoryPath, boolean recursion )
{
	loadDirectoryInternal(GetRootItem(), directoryPath, recursion);
}

void CAdvancedTreeCtrl::loadDirectoryInternal(HTREEITEM item, CString directPath, boolean recursion )
{
	CFileFind file;
	boolean hasFile = file.FindFile(directPath);
	while (hasFile)
	{
		if(file.IsDirectory() && recursion)
		{
			HTREEITEM subTreeItem = InsertItem(file.GetFileName(), item, TVI_LAST);
			loadDirectoryInternal(subTreeItem, file.GetFilePath(), recursion);
		}
		else if(!file.IsDirectory())
		{
			InsertItem(file.GetFileName(), item, TVI_LAST);
		}
		hasFile = file.FindNextFile();
	}
}